package com.jysz.certificate.web.action.mf;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.mf.Bill;
import com.jysz.certificate.service.mf.BillApplyService;
import com.jysz.certificate.service.mf.BillService;
import com.jysz.certificate.utils.CommonUtil;
import com.jysz.certificate.utils.DateUtil;

/**
 * 发票操作控制类(实收、注销、重开、作废重开)
 * @author xuxiaolong
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/mf/core")
public class BillCoreAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	@Resource
	private BillApplyService applyService;
	@Resource
	private BillService billService;
	
	private Long id;
	private Bill billDTO;
	private Bill bill;
	private String ids;
	private String uncheckIds;
	private String hasCharged;
	private String billNum;
	private String billName;
	private Date openDate;
	private Date openDateBegin;//开立发票开始时间
	private Date openDateEnd;//开立发票结束时间
	private Date applyDateBegin;//申请开始时间
	private Date applyDateEnd;//申请结束时间
	private Integer status;//状态
	private String markShop;
	
	//准备实收
	@Action("preIncome")
	public String preIncome(){
		bill=billService.find(id);
		setRedirectUrl(getUrl(),"billDTO",billDTO);
		return "income";
	}
	//实收
	@Action("income")
	public String income(){
		if(CommonUtil.isNotEmpty(ids)){
			Long ary[]=CommonUtil.getLongArrayFromIds(ids, ",");
			billService.income(id, ary);
		}
		setSuccessPopMessage("发票确认实收成功");
		return SHOW_MSG_PAGE;
	}
	
	//准备注销
	@Action("preCancel")
	public String preCancel (){
		bill=billService.find(id);
		setRedirectUrl(getUrl(),"billDTO",billDTO);
		return "cancel";
	}
	//注销
	@Action("cancel")
	public String cancel (){
		billService.cancel(id, CommonUtil.getLongArrayFromIds(ids, ","),billNum,billName,openDate);
		setSuccessPopMessage("发票注销成功");
		return SHOW_MSG_PAGE;
	}
	
	//作废
	public String preInvalid(){
		
		return "";
	}
	//作废
	@Action("invalid")
	public String invalid(){
		billService.invalid(id);
		setSuccessPopMessage("发票作废成功");
		setRedirectUrl("index.do?currentPage="+getCurrentPage());
		return SHOW_MSG_PAGE;
	}
	
	//准备重开
	@Action("preReopen")
	public String preReopen(){
		bill=billService.find(id);
		setRedirectUrl(getUrl(),"billDTO",billDTO);
		return "reopen";
	}
	//重开
	@Action("reopen")
	public String reopen(){
		boolean f=false;
		if("true".equals(hasCharged))
			f=true;
		billService.reopen(id,CommonUtil.getLongArrayFromIds(ids, ","),CommonUtil.getLongArrayFromIds(uncheckIds, ","),f,billNum,billName,openDate);
		setSuccessPopMessage("发票重开成功");
		return SHOW_MSG_PAGE;
	}
	
	//准备 注销重开
	@Action("preCancleReopen")
	public String preCancleReopen(){
		bill=billService.find(id);
		setRedirectUrl(getUrl(),"billDTO",billDTO);
		return "cancleReopen";
	}
	//注销重开
	@Action("cancleReopen")
	public String cancleReopen(){
		boolean f=false;
		if("true".equals(hasCharged))
			f=true;
		billService.cancleReopen(id,CommonUtil.getLongArrayFromIds(ids, ","),CommonUtil.getLongArrayFromIds(uncheckIds, ","),f,billNum,billName,openDate);
		setSuccessPopMessage("发票注销重开成功");
		return SHOW_MSG_PAGE;
	}
	
	private String getUrl(){
		StringBuilder url= new StringBuilder("index.do?currentPage=").append(getCurrentPage());
		if(status!=null)
			url.append("&status=").append(status);
		if(openDateBegin!=null)
			url.append("&openDateBegin=").append(DateUtil.formatDate(openDateBegin, "yyyy-MM-dd"));
		if(openDateBegin!=null)
			url.append("&openDateEnd=").append(DateUtil.formatDate(openDateEnd, "yyyy-MM-dd"));
		if(openDateBegin!=null)
			url.append("&applyDateBegin=").append(DateUtil.formatDate(applyDateBegin, "yyyy-MM-dd"));
		if(openDateBegin!=null)
			url.append("&applyDateEnd=").append(DateUtil.formatDate(applyDateEnd, "yyyy-MM-dd"));
		if(CommonUtil.isNotEmpty(markShop))
			url.append("&markShop=").append(markShop);
		return url.toString();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Bill getBill() {
		return bill;
	}
	public void setBill(Bill bill) {
		this.bill = bill;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getBillNum() {
		return billNum;
	}
	public void setBillNum(String billNum) {
		this.billNum = billNum;
	}
	public String getBillName() {
		return billName;
	}
	public void setBillName(String billName) {
		this.billName = billName;
	}
	public String getUncheckIds() {
		return uncheckIds;
	}
	public void setUncheckIds(String uncheckIds) {
		this.uncheckIds = uncheckIds;
	}
	public String getHasCharged() {
		return hasCharged;
	}
	public void setHasCharged(String hasCharged) {
		this.hasCharged = hasCharged;
	}
	public Date getOpenDate() {
		return openDate;
	}
	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}
	public Bill getBillDTO() {
		return billDTO;
	}
	public void setBillDTO(Bill billDTO) {
		this.billDTO = billDTO;
	}
	public Date getOpenDateBegin() {
		return openDateBegin;
	}
	public void setOpenDateBegin(Date openDateBegin) {
		this.openDateBegin = openDateBegin;
	}
	public Date getOpenDateEnd() {
		return openDateEnd;
	}
	public void setOpenDateEnd(Date openDateEnd) {
		this.openDateEnd = openDateEnd;
	}
	public Date getApplyDateBegin() {
		return applyDateBegin;
	}
	public void setApplyDateBegin(Date applyDateBegin) {
		this.applyDateBegin = applyDateBegin;
	}
	public Date getApplyDateEnd() {
		return applyDateEnd;
	}
	public void setApplyDateEnd(Date applyDateEnd) {
		this.applyDateEnd = applyDateEnd;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getMarkShop() {
		return markShop;
	}
	public void setMarkShop(String markShop) {
		this.markShop = markShop;
	}
	

}
